package com.epfresh.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.adapter.CommonAdapter;
import com.epfresh.api.adapter.ViewHolder;
import com.epfresh.api.constant.AppPurchaseRouterConstant;
import com.epfresh.api.entity.*;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.FormatUtil;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.api.widget.NoScrollListView;
import com.epfresh.api.widget.lines.Donut;
import com.epfresh.api.widget.lines.DonutChart;
import com.epfresh.api.widget.lines.SegmentLines;
import com.epfresh.api.widget.lines.Unit;
import com.epfresh.bean.BookHome;
import com.epfresh.global.BaseActivity;
import com.google.gson.Gson;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BookActivity extends BaseActivity {

    private TextView tvPay, tvPayNo, tvTime;//, tvDealTime;
    SegmentLines segmentLines;
    TextView tvMoreDetail;
    View ivTime, tv_pay_look;
    NoScrollListView lv_deal_detail;
    int timeYearFlag = 1; //年月类型；1：月；2：年
    String timeString = "";//年月字符串；年:"2017"；月:"20174-04"
    public String timeTitle = "";
    ArrayList<BookHome.DetailListBean> detailList = new ArrayList<>();
    DetailAdapter detailAdapter;
    View rlTime;
    ListView lvTime;
    TimeAdapter timeAdapter;
    ArrayList<BookHome.QueryDateListBean> timeList = new ArrayList<>();
    int timePosition;
    View iv_left;
    View iv_right;
    View ll_pay_no,ll_pay;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book);
        ivBack.setVisibility(View.VISIBLE);
        toolbarTitle.setText("我的台账");
        rightbtn.setText("每周明细");
        rightbtn.setOnClickListener(this);
        rightbtn.setTextSize(TypedValue.COMPLEX_UNIT_PX,rightbtn.getTextSize()-4);
        rightbtn.setTextColor(Color.parseColor("#3eb049"));
        rightbtn.setVisibility(View.VISIBLE);
        lv_deal_detail = (NoScrollListView) findViewById(R.id.lv_deal_detail);
        detailAdapter = new DetailAdapter(this, detailList, R.layout.item_book_detail);
        lv_deal_detail.setAdapter(detailAdapter);

        tv_pay_look = findViewById(R.id.tv_pay_look);
        tv_pay_look.setOnClickListener(this);

        ll_pay_no = findViewById(R.id.ll_pay_no);
        ll_pay_no.setOnClickListener(this);
        ll_pay = findViewById(R.id.ll_pay);
        ll_pay.setOnClickListener(this);

        tvPayNo = (TextView) findViewById(R.id.tv_pay_no);
        tvPay = (TextView) findViewById(R.id.tv_pay);
        tvTime = (TextView) findViewById(R.id.tv_time);
        tvMoreDetail = (TextView) findViewById(R.id.tv_more_detail);
        ivTime = findViewById(R.id.iv_time);
        ivTime.setOnClickListener(this);
        tvMoreDetail.setOnClickListener(this);

        rlTime = findViewById(R.id.rl_time);
        lvTime = (ListView) findViewById(R.id.lv_time);
        timeAdapter = new TimeAdapter(this, timeList, R.layout.item_string_book);
        lvTime.setAdapter(timeAdapter);
        rlTime.setOnClickListener(this);

        segmentLines = (SegmentLines) findViewById(R.id.segment_line);
        int fullColor = 0xff009983;
        int circleCurrentColor = 0x6099dfdf;
        segmentLines.setShaderColor(new int[]{0xff4ee4e3, 0xff87edec, 0xffcaf7f7, 0xffeefcfc,0xffffffff});
        segmentLines.setFullColor(fullColor);
        segmentLines.setCircleCurrentColor(circleCurrentColor);
        iv_left = findViewById(R.id.iv_left);
        iv_right = findViewById(R.id.iv_right);
        iv_right.setOnClickListener(this);
        iv_left.setOnClickListener(this);
        mHttpLoad();
    }

    private void initSegment() {
        if (data == null) {
            segmentLines.setVisibility(View.INVISIBLE);
            return;
        }
        List<Unit> lines = new ArrayList<>();
        List<BookHome.TrendListBean> trendList = data.getTrendList();
        int position = 0;
        boolean isFirstPoint = false;
        if(trendList == null || trendList.size() == 0){
            segmentLines.setVisibility(View.INVISIBLE);
        }
        if (trendList != null && trendList.size() > 0) {
            if (trendList.size() < 5) {
                segmentLines.setVisibility(View.INVISIBLE);
                return;
            }
            segmentLines.setVisibility(View.VISIBLE);
            position = trendList.size() - 1;
            for (int i = 0; i < trendList.size(); i++) {
                BookHome.TrendListBean trendListBean = trendList.get(i);

                if (trendListBean != null) {
                    String name = trendListBean.getName();
                    String type = trendListBean.getType();
                    String price = trendListBean.getPrice();
                    double priceY = trendListBean.getPriceY();
                    String queryDate = trendListBean.getQueryDate();

                    if ("1".equals(type)) {
                        lines.add(new Unit((float) priceY, name, price));
                    } else if ("0".equals(type)) {
                        if (!isFirstPoint) {
                            isFirstPoint = true;
                            if (i > 0)
                                position = i - 1;
                            lines.add(new Unit((float) priceY, name, price, 1));
                        } else {
                            lines.add(new Unit((float) priceY, name, price, 1));
                        }
                    }
                }
            }
        } else {
            return;
        }
        if (position < lines.size()) {
            Unit unit = lines.get(position);
            unit.setPointType(2);
        }

        segmentLines.feed(lines, position);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);

        switch (view.getId()) {
            case R.id.rightbtn:
                Intent detail = new Intent(mContext,
                        BookDetailWeekActivity.class);
/*                detail.putExtra("timeTitle", tvTime.getText().toString());
                detail.putExtra("timeValue", timeString);
                detail.putExtra("money", "" + data.getQueryTotleIncome());*/
                startActivity(detail);
                break;

            case R.id.t_tag:
                Object o = view.getTag(R.id.item_key_position);
                if (o != null && o instanceof Integer) {
                    int position = (int) o;
                    if(timeYearFlag==2) {
//                        toast("这是年之"+position);
                        Intent detailIntent = new Intent(this,
                                BookGoodsYearActivity.class);
                        detailIntent.putExtra("nameTitle", data.getTopCategoryList().get(position).getCategoryName());
                        detailIntent.putExtra("timeValue", data.getTopCategoryList().get(position).getQueryDate());
                        detailIntent.putExtra("categoryIds", data.getTopCategoryList().get(position).getCategoryIds());
                        startActivity(detailIntent);
                    }else{
                        //toast("这是月"+position);
                        Intent detailIntent = new Intent(this,
                                BookGoodsMonthActivity.class);
                        detailIntent.putExtra("nameTitle", data.getTopCategoryList().get(position).getCategoryName());
                        detailIntent.putExtra("timeValue", data.getTopCategoryList().get(position).getQueryDate());
                        detailIntent.putExtra("categoryIds", data.getTopCategoryList().get(position).getCategoryIds());
                        startActivity(detailIntent);
                    }
                }
                break;

            case R.id.ll_pay:
                Intent payIntent = new Intent(mContext,
                        BookPayActivity.class);
                payIntent.putExtra("timeValue", timeString);
                payIntent.putExtra("type", 1);
                startActivity(payIntent);

                break;
            case R.id.ll_pay_no:
            case R.id.tv_pay_look:
                Intent payNoIntent = new Intent(mContext,
                        BookPayActivity.class);
                payNoIntent.putExtra("timeValue", timeString);
                Log.e("timeValue",""+timeString);

                startActivity(payNoIntent);

                break;
            case R.id.iv_right:
                rlTime.setVisibility(View.GONE);
                if(timePosition<=0){

                }else{

                    timePosition--;
                    timeString = timeList.get(timePosition).getQueryDate();
                    //tvTime.setText(timeList.get(timePosition).getName());
                    timeYearFlag = 2;
                    if(timePosition == 0){
                        iv_right.setVisibility(View.GONE);
                    }else{
                        iv_right.setVisibility(View.VISIBLE);
                    }
                    if(timePosition == timeList.size()-1 ){
                        iv_left.setVisibility(View.GONE);
                    }else {
                        iv_left.setVisibility(View.VISIBLE);
                    }
                    mHttpLoad();
                }
                break;
            case R.id.iv_left:
                rlTime.setVisibility(View.GONE);
                if(timePosition>=timeList.size()-1){

                }else{
                    timePosition++;
                    timeString = timeList.get(timePosition).getQueryDate();
                    //tvTime.setText(timeList.get(timePosition).getName());
                    timeYearFlag = 1;
                    if(timePosition == 0){
                        iv_right.setVisibility(View.GONE);
                    }else{
                        iv_right.setVisibility(View.VISIBLE);
                    }
                    if(timePosition == timeList.size()-1 ){
                        iv_left.setVisibility(View.GONE);
                    }else {
                        iv_left.setVisibility(View.VISIBLE);
                    }
                    mHttpLoad();
                }
                break;
/*            case R.id.ll_best_item:
                Object o = view.getTag(R.id.item_key_position);
                if (o != null && o instanceof Integer) {
                    int position = (int) o;
                    if(timeYearFlag==2) {
                        Intent detailIntent = new Intent(this,
                                BookGoodsYearActivity.class);
                        detailIntent.putExtra("timeTitle", timeTitle);
                        detailIntent.putExtra("timeValue", bestList.get(position).getQueryDate());
                        detailIntent.putExtra("money", "" + bestList.get(position).getPrice() + "元");
                        detailIntent.putExtra("productId", bestList.get(position).getSpecId());
                        startActivity(detailIntent);
                    }else{
                        Intent detailIntent = new Intent(this,
                                BookGoodsMonthActivity.class);
                        detailIntent.putExtra("timeTitle", timeTitle);
                        detailIntent.putExtra("timeValue", bestList.get(position).getQueryDate());
                        detailIntent.putExtra("money", "" + bestList.get(position).getPrice() + "元");
                        detailIntent.putExtra("productId", bestList.get(position).getSpecId());
                        startActivity(detailIntent);
                    }
                }
                break;*/
            case R.id.rl_time:
                rlTime.setVisibility(View.GONE);
                break;
            case R.id.iv_time:
                // 返回上一页
                if (rlTime.getVisibility() == View.VISIBLE) {
                    rlTime.setVisibility(View.GONE);
                } else {
                    rlTime.setVisibility(View.VISIBLE);
                    timeAdapter.notifyDataSetChanged();
                }
                break;
/*            case R.id.tv_more_best:
                Intent bestIntent = new Intent(mContext,
                        BookBestMoreActivity.class);
                bestIntent.putExtra("timeTitle", tvTime.getText().toString());
                bestIntent.putExtra("timeValue", timeString);
                bestIntent.putExtra("money", data.getQueryTotleIncome());
                startActivity(bestIntent);
                break;*/
            case R.id.tv_more_detail:
                if (timeYearFlag == 1) {
                    Intent detailIntent = new Intent(mContext,
                            BookDetailMonthActivity.class);
                    detailIntent.putExtra("timeTitle", tvTime.getText().toString());
                    detailIntent.putExtra("timeValue", timeString);
                    detailIntent.putExtra("money", "" + data.getQueryTotleIncome());
                    startActivity(detailIntent);
                }
                break;

/*                    detailIntent.putExtra("timeTitle", detailList.get(position).getName());
                    detailIntent.putExtra("timeValue", detailList.get(position).getQueryDate());
                    detailIntent.putExtra("money", detailList.get(position).getPrice());
                }else{
                    Intent detailIntent = new Intent(mContext,
                            BookMonthDetailActivity.class);
                    detailIntent.putExtra("timeTitle", tvTime.getText().toString());
                    detailIntent.putExtra("timeValue", timeString);
                    detailIntent.putExtra("money", data.getQueryTotleIncome());
                    startActivity(detailIntent);
                }
                break;*/
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    /**
     * 加载台账数据
     */
    private void mHttpLoad() {
        RequestEntityPurchase<Map<String, String>> data = new RequestEntityPurchase<>();
        data.setCmd(AppPurchaseRouterConstant.cmd_order_income_incomeMain);
        Map<String, String> map = new HashMap<>();
        map.put("queryDate", timeString);
        map.put("accountId", getUser().getAccountId());
        data.setParameters(map);
        request(data, AppPurchaseRouterConstant.cmd_order_income_incomeMain, onRequestListener);
    }

    BookHome data;
    private OnRequestListener<BookHome> onRequestListener = new OnRequestListener<BookHome>() {
        @Override
        public BookHome jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, BookHome.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<BookHome> response, Object tag) {

            hideProgressDialog();
            BookHome responseElement = response.getResponseElement();
            data = responseElement;
            timeString = responseElement.getQueryDate();
            tvTime.setText(responseElement.getQueryDateTitle());
            timeTitle = responseElement.getQueryDateTitle();
            detailList.clear();
            timeList.clear();
            if (timeYearFlag == 2) {//年的详情数据显示全部；月的详情，最多七条，超过了显示更多；
                //tvMoreDetail.setVisibility(View.GONE);
                tvMoreDetail.setText("没有更多了");
                tvMoreDetail.setClickable(false);
            } else {
                if (responseElement.getDetailList().size() <= 7) {
                    //tvMoreDetail.setVisibility(View.GONE);
                    tvMoreDetail.setText("没有更多了");
                    tvMoreDetail.setClickable(false);
                } else {
                    //tvMoreDetail.setVisibility(View.VISIBLE);
                    tvMoreDetail.setText("查看更多");
                    tvMoreDetail.setClickable(true);
                }
            }
            detailList.addAll(responseElement.getDetailList());
            detailAdapter.notifyDataSetChanged();
            for (int i = 0; i < responseElement.getQueryDateList().size(); i++) {
                timeList.add(responseElement.getQueryDateList().get(i));
                timeList.addAll(responseElement.getQueryDateList().get(i).getChild());
            }
            timeAdapter.notifyDataSetChanged();
            initSegment();
            initButtons();
            initDonutChart();
/*            tvPay.setText(responseElement.getPayedPrice());
            tvPayNo.setText(responseElement.getUnPayPrice());*/
            tvPay.setText(FormatUtil.formatDecimal(responseElement.getPayedPrice()));
            tvPayNo.setText(FormatUtil.formatDecimal(responseElement.getUnPayPrice()));
            for(int i=0;i<timeList.size();i++){
                if(timeList.get(i).getQueryDate().equals(responseElement.getQueryDate())){
                    timePosition = i;
                    return;
                }
            }
        }
    };
    //DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");//格式化设置

    class DetailAdapter extends CommonAdapter<BookHome.DetailListBean> implements View.OnClickListener {
        public DetailAdapter(Context context, ArrayList<BookHome.DetailListBean> runPacks, int a) {
            super(context, runPacks, a);
        }

        public void onClick(View v) {
            Object tag = v.getTag(R.id.item_key_position);
            if (tag != null && tag instanceof Integer) {
                int position = (int) tag;
                if (timeYearFlag == 2) {
                    Intent detailIntent = new Intent(mContext,
                            BookDetailMonthActivity.class);
                    detailIntent.putExtra("timeTitle", detailList.get(position).getName());
                    detailIntent.putExtra("timeValue", detailList.get(position).getQueryDate());
                    detailIntent.putExtra("money", detailList.get(position).getPrice());
                    //detailIntent.putExtra("detailList", (Serializable) detailList);
                    startActivity(detailIntent);
                } else {
                    Intent detailIntent = new Intent(mContext,
                            BookDetailDayActivity.class);
                    detailIntent.putExtra("timeTitle", detailList.get(position).getName());
                    detailIntent.putExtra("timeValue", detailList.get(position).getQueryDate());
                    detailIntent.putExtra("money", detailList.get(position).getPrice());
                    //detailIntent.putExtra("detailList",(Serializable) detailList);
                    startActivity(detailIntent);
                }
            }
        }

        @Override
        public int getCount() {
            if (mDatas == null) {
                return 0;
            }
            if (timeYearFlag == 1 && mDatas.size() > 7) {
                return 7;
            } else {
                return mDatas.size();
            }
        }

        @Override
        public void convert(final ViewHolder holder, final BookHome.DetailListBean bean) {
            holder.getView(R.id.ll_item).setTag(R.id.item_key_position, detailList.indexOf(bean));
            holder.setOnClickListener(R.id.ll_item, this);
            holder.setText(R.id.tv_time, bean.getName());
            holder.setText(R.id.tv_money, FormatUtil.formatDecimal(bean.getPrice()) + "元");
        }
    }


    class TimeAdapter extends CommonAdapter<BookHome.QueryDateListBean> implements View.OnClickListener {
        public TimeAdapter(Context context, ArrayList<BookHome.QueryDateListBean> runPacks, int a) {
            super(context, runPacks, a);
        }

        public void onClick(View v) {
            Object tag = v.getTag(R.id.item_key_position);
            if (tag != null && tag instanceof Integer) {
                timePosition = (int) tag;
                rlTime.setVisibility(View.GONE);
                if (timeString.equals(timeList.get(timePosition).getQueryDate())) {
                    return;//如果点选的是当前时间，直接取消对话框即可
                }
                if (timeList.get(timePosition).getQueryDate().length() == 4) {
                    timeYearFlag = 2;
                } else {
                    timeYearFlag = 1;
                }
                timeString = timeList.get(timePosition).getQueryDate();
                //tvTime.setText(timeList.get(timePosition).getName());
                if(timePosition == 0){
                    iv_right.setVisibility(View.GONE);
                }else{
                    iv_right.setVisibility(View.VISIBLE);
                }
                if(timePosition == timeList.size()-1 ){
                    iv_left.setVisibility(View.GONE);
                }else {
                    iv_left.setVisibility(View.VISIBLE);
                }
                mHttpLoad();
            }
        }

        @Override
        public void convert(final ViewHolder holder, final BookHome.QueryDateListBean bean) {
            holder.getView(R.id.tv_string).setTag(R.id.item_key_position, timeList.indexOf(bean));
            holder.setOnClickListener(R.id.tv_string, this);
            holder.setText(R.id.tv_string, bean.getName());
            if(timeList.indexOf(bean)==timePosition){
                holder.getView(R.id.v_flag).setVisibility(View.VISIBLE);
                ((TextView)holder.getView(R.id.tv_string)).setTextColor(Color.parseColor("#3eb049"));
            }else{
                holder.getView(R.id.v_flag).setVisibility(View.INVISIBLE);
                ((TextView)holder.getView(R.id.tv_string)).setTextColor(Color.parseColor("#434343"));
            }
        }
    }



    void initButtons(){

//        int roundRadius = 15; // 8px not dp
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout ll_button = (LinearLayout)findViewById(R.id.ll_button);
        ll_button.removeAllViews();
        List<BookHome.TopCategoryListBean> topCategoryList = null;
        if (data != null){
            topCategoryList = data.getTopCategoryList();

        }
        int w = LocalDisplay.SCREEN_WIDTH_PIXELS;
        w -= LocalDisplay.dp2px(20);
        int textW = (w - LocalDisplay.dp2px(28))/5;
        if (topCategoryList == null || topCategoryList.size() == 0) {
            GradientDrawable gd = (GradientDrawable) getResources().getDrawable(R.drawable.book_btn_six);
            TextView flag = new TextView(this);
            flag.setWidth(textW);
            flag.setGravity(Gravity.CENTER);
            flag.setLines(1);
            flag.setText("没有消费");
            flag.setTextSize(10);
            //flag.setPadding(10,5,10,5);
            lp.setMargins(0, 0, LocalDisplay.dp2px(7), 0);
            flag.setLayoutParams(lp);
            flag.setTextColor(Color.WHITE);
            gd.setColor(0xffdcdcdc);
            flag.setBackgroundDrawable(gd);
            ll_button.addView(flag);
            flag.setId(R.id.t_tag);
            flag.setTag(R.id.item_key_position, 0);
//            flag.setOnClickListener(null);
        }else {


            int colors[] = {Color.parseColor("#9c8dd0"), Color.parseColor("#fc6817"), Color.parseColor("#fec004"), Color.parseColor("#10a7eb"), Color.parseColor("#65d2d7"), Color.parseColor("#40b3f6"), Color.parseColor("#84e3ef")};
            for (int i = 0; i < topCategoryList.size(); i++) {
                if (i < 5) {
                    GradientDrawable gd = (GradientDrawable) getResources().getDrawable(R.drawable.book_btn_six);

//            gd.setCornerRadius(roundRadius);
                    TextView flag = new TextView(this);

                    flag.setWidth(textW);
                    flag.setEllipsize(TextUtils.TruncateAt.END);
                    flag.setGravity(Gravity.CENTER);
                    flag.setLines(1);
                    flag.setText(topCategoryList.get(i).getCategoryName());
                    flag.setTextSize(10);
                    //flag.setPadding(10,5,10,5);
                    lp.setMargins(0, 0, LocalDisplay.dp2px(7), 0);
                    flag.setLayoutParams(lp);
                    flag.setTextColor(Color.WHITE);
                    gd.setColor(colors[i]);
                    flag.setBackgroundDrawable(gd);
                    ll_button.addView(flag);
                    flag.setId(R.id.t_tag);

                    flag.setTag(R.id.item_key_position, i);
                    flag.setOnClickListener(this);
                }
            }
        }
    }

    ViewGroup vDonut;
    DonutChart donutChart;
    private void initDonutChart() {
        if (vDonut == null) {
            vDonut = (ViewGroup) findViewById(R.id.ll_donut);
        }

        int colors[] = {Color.parseColor("#9c8dd0"), Color.parseColor("#fc6817"), Color.parseColor("#fec004"), Color.parseColor("#10a7eb"), Color.parseColor("#65d2d7"), Color.parseColor("#40b3f6"), Color.parseColor("#84e3ef")};
        ArrayList<Donut> ds = new ArrayList<>();
        List<BookHome.TopCategoryListBean> topCategoryList = null;
        String queryTotleIncome = null;
        if (data != null){
            topCategoryList = data.getTopCategoryList();
            queryTotleIncome = data.getQueryTotleIncome();

        }
//        queryTotleIncome = "9999999999.99";
        if (topCategoryList == null || topCategoryList.size() == 0) {
            Donut donut = new Donut();
            donut.setName("");
            donut.setRatio(1 + "");
            donut.setColor(0xffdcdcdc);
            ds.add(donut);
            findViewById(R.id.rl_pay_info).setVisibility(View.GONE);
            findViewById(R.id.tv_detail).setVisibility(View.GONE);
/*            tvMoreDetail.setText("没有更多了");
            tvMoreDetail.setClickable(false);*/
        } else {
            findViewById(R.id.rl_pay_info).setVisibility(View.VISIBLE);
            findViewById(R.id.tv_detail).setVisibility(View.VISIBLE);
/*            tvMoreDetail.setText("查看更多");
            tvMoreDetail.setClickable(true);*/
            for (int i = 0; i < topCategoryList.size(); i++) {
                if (i < 5){
                    BookHome.TopCategoryListBean tc = topCategoryList.get(i);
                    Donut donut = new Donut();
                    donut.setName(tc.getCategoryName());
                    donut.setRatio(tc.getRatio());
                    donut.setColor(colors[i]);
                    ds.add(donut);
                }
            }
        }

        if (donutChart == null) {
            donutChart = new DonutChart(this, FormatUtil.formatDecimal(queryTotleIncome), 0xffffffff, calculateDegree(ds));
            vDonut.addView(donutChart);
        }else {
            donutChart.startDonut(FormatUtil.formatDecimal(queryTotleIncome), calculateDegree(ds));
        }

/*
        if (donutChart == null) {
            donutChart = new DonutChart(this, decimalFormat.format(queryTotleIncome), 0xffffffff, calculateDegree(ds));
            vDonut.addView(donutChart);
        }else {
            donutChart.startDonut(decimalFormat.format(queryTotleIncome), calculateDegree(ds));
        }
* */

    }

    private ArrayList<Donut> calculateDegree(ArrayList<Donut> data) {
        float total = 0;
        for (int i = 0; i < data.size(); i++) {
            Donut donut = data.get(i);
            total += donut.getRatioFloat();
        }
        if (total == 0) {

        }
        for (int i = 0; i < data.size(); i++) {
            Donut donut = data.get(i);
            donut.setDegree(360 * (donut.getRatioFloat() / total));
        }

        return data;
    }

}
